package org.kernely.security.ui;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.kernely.core.service.KernelyBundleService;
import org.kernely.security.Activator;
import org.kernely.security.service.KernelySecurityService;
import org.kernely.security.ui.administration.SecurityAdministrationPresenter;
import org.kernely.security.ui.administration.SecurityAdministrationView;
import org.kernely.security.ui.user.mgmt.UserManagementPresenter;
import org.kernely.security.ui.user.mgmt.UserManagementView;
import org.kernely.ui.mvp.IKernelyView;
import org.kernely.ui.mvp.KernelyBundleMenu;
import org.kernely.ui.mvp.KernelyEventBus;
import org.kernely.ui.mvp.KernelyViewPresenterManager;
import org.kernely.ui.services.KernelyUIBundle;
import org.kernely.ui.services.KernelyUIBundleService;
import org.osgi.service.event.EventAdmin;

import com.vaadin.Application;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.Resource;

public class SecurityUIBundle implements KernelyUIBundle {
	
	public static final String NEW_USER = "NewUser"; // Used as a title for events, when a new user is added.
	public static final String EDIT_USER = "EditUser"; // Used as a title for events, when a user is edited.

	/**
	 * The UI registering service
	 */
	private static KernelyUIBundleService uiBundleService;
	
	/**
	 * The event admin service, to send "system" event to other bundles.
	 */
	private static EventAdmin eventAdmin;

	/**
	 * The security service : retrieving current user, roles...
	 */
	private static KernelySecurityService securityService;

	/**
	 * @see org.kernely.core.service.KernelyBundle#defaultParentMenuItem()
	 */
	public final boolean defaultParentMenuItem() {
		return false;
	}

	/**
	 * @see org.kernely.core.service.KernelyBundle#getMenu()
	 */
	public final KernelyBundleMenu getMenu() {
		return null;
	}

	public static KernelyEventBus getEventBus() {
		Session session = (Session) SecurityUtils.getSubject().getSession();
		return (KernelyEventBus) session.getAttribute("eventBus");
		
	}

	/**
	 * @see org.kernely.core.service.KernelyBundle#getName()
	 */
	public final String getName() {
		return "Security Bundle";
	}

	/**
	 * @see org.kernely.core.service.KernelyBundle#getResource(Application)
	 */
	public final Resource getIcon() {
		return new ExternalResource("securityUIbundle/images/security.png");
	}

	/**
	 * @see org.kernely.core.service.KernelyBundle#getViewPresenter()
	 */
	public final KernelyViewPresenterManager getViewPresenter() {
		KernelyViewPresenterManager hm = new KernelyViewPresenterManager();
		// Adding all associations View - Presenter
		hm.put(UserManagementView.class, UserManagementPresenter.class);
		hm.put(SecurityAdministrationView.class, SecurityAdministrationPresenter.class);
		return hm;
	}
	
	/**
	 * Gets the security service.
	 * @return the security service.
	 */
	public static KernelySecurityService getSecurityService() {
		return securityService;
	}

	/**
	 * Method called by the Service Component Runtime
	 * 
	 * @param service
	 *            : the KernelyBundleService needed to register this bundle
	 */
	public final void setModuleService(KernelyBundleService service) {
		// log.log(LogService.LOG_DEBUG,
		// "Try to register with KernelyBundleService : "+service);
		// Registering the bundle
		service.registerModule(this);
		// log.log(LogService.LOG_INFO,
		// "Registering with KernelyBundleService");
	}

	/**
	 * Method called by the Service Component Runtime
	 * 
	 * @param service
	 *            : the KernelyBundleService needed to unregister this bundle
	 */
	public final void unsetModuleService(KernelyBundleService service) {
		// Unregistering the bundle
		service.unregisterModule(this);
		// log.log(LogService.LOG_INFO,
		// "Unregistering with KernelyBundleService");
	}

	/**
	 * Sets the KernelyUIBundleService Declarative Service.
	 * 
	 * @param uiBundleService
	 *            : the KernelyUIBundleService service
	 */
	public final void setUIBundleService(KernelyUIBundleService uiBundleService) {
		// log.log(LogService.LOG_INFO,
		// "Registering with KernelyUIBundleService");
		SecurityUIBundle.uiBundleService = uiBundleService;
		SecurityUIBundle.uiBundleService.registerModule(this);
	}

	/**
	 * Unsets the KernelyUIBundleService Declarative Service.
	 * 
	 * @param uiBundleService
	 *            : the KernelyUIBundleService service
	 */
	public final void unsetUIBundleService(KernelyUIBundleService service) {
		// log.log(LogService.LOG_INFO,
		// "Unregistering with KernelyUIBundleService");
		SecurityUIBundle.uiBundleService.unregisterModule(this);
	}
	
	/**
	 * Sets the security Declarative Service
	 * @param service The security service.
	 */
	public final void setSecurityService(KernelySecurityService service) {
		securityService = service;
	}

	/**
	 * Unsets the security Declarative Service
	 * @param service The security service.
	 */
	public final void unsetSecurityService(KernelySecurityService service) {}

	/**
	 * @see
	 */
	public final Class<? extends IKernelyView> getAdministrationView() {
		return SecurityAdministrationView.ID_VIEW;
	}

	
	/*
	 * (non-Javadoc)
	 * @see org.kernely.ui.services.KernelyUIBundle#getMenuIcon(com.vaadin.Application)
	 */
	@Override
	public final Resource getMenuIcon() {
		// This bundle doesn't have a specific menu, so it doesn't have menu icon
		return null;
	}
	
	

	
	/**
	 * Sets the event admin service.
	 * 
	 * @param eventAdminService
	 *            : the felix event admin service
	 */
	public static void setEventAdminService(EventAdmin eventAdminService) {
		SecurityUIBundle.eventAdmin = eventAdminService;
	}

	/**
	 * Gets the event admin service.
	 * @return 
	 * 
	 * @return the event admin service
	 */
	public static EventAdmin getEventAdminService() {
		return SecurityUIBundle.eventAdmin;
	}
}
